<?php
class dbConnection
{
	var $connection;
	var $databaseName;
	var $result;

	// -------------------------------------------------------------
	function dbConnection()
	{
		$this->databaseName = $GLOBALS['configuration']['db'];
		$serverName = $GLOBALS['configuration']['host'];
		$databaseUser = $GLOBALS['configuration']['user'];
		$databasePassword = $GLOBALS['configuration']['pass'];
		$this->connection = mysql_connect ($serverName, $databaseUser, $databasePassword);
		if ($this->connection)
		{
			if (!mysql_select_db ($this->databaseName))
			{
				throw new Exception('I cannot find the specified database "'.$this->databaseName.'". Please edit configuration.php.');
			}
		}
		else
		{
			throw new Exception('I cannot connect to the database. Please edit configuration.php with your database configuration.');
		}
	}

	// -------------------------------------------------------------
	function close()
	{
		mysql_close($this->connection);
	}

	// -------------------------------------------------------------
	function connect() {
		return $this->connection;
	}

	// -------------------------------------------------------------
	function query($query)
	{
		$this->result = mysql_query($query,$this->connection);
		if (!$this->result) {
			throw new Exception(mysql_errno().":".mysql_error()." on sql: ".$query);
		}
		return $this->result;
	} 

	// -------------------------------------------------------------
	function rows()
	{
		if ($this->result != false)
		{
			return mysql_num_rows($this->result);
		}
		return null;
	}

	// -------------------------------------------------------------
	function affectedRows()
	{
		return mysql_affected_rows();
	}

	// -------------------------------------------------------------
	function result($row,$name)
	{
		if ($this->Rows() > 0)
		{
			return mysql_result($this->result,$row,$name);
		}
		return null;
	}

	// -------------------------------------------------------------
	function insertOrUpdate($query)
	{
		$this->result = mysql_query($query,$this->connection);
		return ($this->AffectedRows() > 0);
	}

	/**
	* This function will try to encode $text to base64, except when $text is a number. This allows us to Escape all data before they're inserted in the database, regardless of attribute type.
	* @param string $text
	* @return string encoded to base64
	*/
	function escape($text)
	{
		if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text))
		{
			return base64_encode($text);
		}
		return mysql_escape_string($text);
	}

	// -------------------------------------------------------------
	function unescape($text)
	{
		if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text))
		{
			return base64_decode($text);
		}
		return stripcslashes($text);
	}

	// -------------------------------------------------------------
	function getCurrentId()
	{
		return intval(mysql_insert_id($this->connection));
	}
}
?>
